
import BaseUI from "../../framwork/BaseUI";
import GameData from "../data/GameData";
import MonsterManager from "./MonsterManager";

const {ccclass, property} = cc._decorator;

@ccclass
export default class PlayerManager extends BaseUI {
    private static _instance: PlayerManager = null;
    public static getInstance(): PlayerManager {
        return PlayerManager._instance;
    }
    baseUI_onLoad(): void {
        PlayerManager._instance = this;
        this.initPlayer();
    }
    playerInfo = {
        MaxHp:0,
        Hp:0,
        atk:0,
        GS:0,
    }
    initPlayer(){
        this.playerInfo.MaxHp = Number(GameData.playerData.HP);
        this.playerInfo.Hp = Number(GameData.playerData.HP);
        this.playerInfo.atk = Number(GameData.playerData.ATK);
        this.playerInfo.GS = Number(GameData.playerData.GS);
    }
    update(dt: number): void {
        this.playerAtt(dt);
    }
    playerTimer: number = 0;
    playerAtt(dt: number){
        this.playerTimer += dt;
        if(this.playerTimer >= this.playerInfo.GS){
            this.playerTimer = 0;
            let playerNode:cc.Node = this.GetGameObject("player");
            cc.tween(playerNode).by(0.1,{x:10}).by(0.1,{x:-10})
            .call(()=>{
                MonsterManager.getInstance().BeingBeaten(this.playerInfo.atk);
                this.monsterInfo.Hp -= this.playerInfo.atk;
                this.resetView();
                if(this.monsterInfo.Hp <= 0){
                    Toast.show("怪死了");
                    this.init();
                }
            })
            .start();
            console.log("玩家攻击")
        }
    }
    baseUI_onDestroy(): void {

    }
    onBtn_click(target: cc.Button, onBtn_name: string): number | void {

    }
    init_data(d: any): void {

    }

}
